<template>
  <div class="app-container">
    <div class="header-search">
      <div class="header-search-title">操作：</div>
      <div class="item">
        <div class="item-other">
          <el-button
            @click="addLine"
            class="search-button"
            size="mini"
            type="success"
            >添加
          </el-button>
          <el-button
            @click="deleteMore"
            class="search-button"
            size="mini"
            type="danger"
            >批量删除（{{ selectNum }}）
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      :data="tableData"
      height="620"
      style="width: 100%; height: 518px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="序号" type="index"> </el-table-column>
      <el-table-column label="ID" prop="id" width="150px" align="center">
      </el-table-column>
      <el-table-column label="配置路径" prop="url"> </el-table-column>
      <el-table-column label="创建时间" prop="createTime"> </el-table-column>
      <el-table-column label="珍贵程度">
        <template slot-scope="scope">
          {{ scope.row["state"] === 0 ? "未配置" : "默认配置" }}
        </template>
      </el-table-column>
      <el-table-column width="300" align="center" label="操作">
        <template slot-scope="scope">
          <div
            style="display: flex; justify-content: center"
            v-if="scope.row['state'] == 0"
          >
            <el-button
              size="mini"
              type="primary"
              @click="handleDefault(scope.$index, scope.row)"
            >
              设置为默认
            </el-button>
            <el-button
              size="mini"
              type="success"
              @click="handleUpdate(scope.$index, scope.row)"
            >
              修改
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div class="footer-pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page="pageNum"
        :page-count="totalPages"
        @current-change="change"
        :pager-count="9"
      >
      </el-pagination>
    </div> -->
    <update-pop :show.sync="updateShow" :item.sync="updateItem" />
  </div>
</template>

<script>
import {
  getQRcode,
  settingQrCodeUrl,
  addQrCodeUrl,
  deleteQrCodeUrl,
} from "@api/system";
import { Loading } from "element-ui";
import UpdatePop from "./components/update.vue";
export default {
  components: {
    UpdatePop,
  },
  name: "List",
  filters: {
    getRealTime(e) {
      console.log(e);
      if (e) {
        const time = new Date(e);
        const year = time.getFullYear();
        const _month = time.getMonth() + 1;
        const month = _month < 10 ? "0" + _month : _month;
        const day = time.getDay() < 10 ? "0" + time.getDay() : time.getDay();
        const hours =
          time.getHours() < 10 ? "0" + time.getHours() : time.getHours();
        const min =
          time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes();
        const second =
          time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
        return (
          year +
          "-" +
          month +
          "-" +
          day +
          " " +
          hours +
          ":" +
          min +
          ":" +
          second
        );
      }
      return "";
    },
  },
  data() {
    return {
      pageNum: 1,
      pageSize: 20,
      totalSize: 0,
      totalPages: 0,
      tableData: [],
      search: "",
      searchLabel: "",
      options: [
        {
          value: "",
          label: "请选择查询条件",
        },
        {
          value: "id",
          label: "编号",
        },
        {
          value: "grade",
          label: "等级",
        },
        {
          value: "chineseName",
          label: "中文名",
        },
        {
          value: "latinName",
          label: "拉丁名",
        },
      ],
      value: "",
      selectInput: "",
      updateShow: false,
      updateItem: {},
      select: [],
    };
  },
  created() {
    this.geList();
  },
  computed: {
    selectNum() {
      return this.select.length;
    },
  },
  methods: {
    deleteMore() {
      if (this.selectNum === 0) return this.$message.warning("您未需选择");
      this.$confirm("此操作将删除所选的用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let loadingInstance = Loading.service({
            fullscreen: true,
          });
          deleteQrCodeUrl(this.select)
            .then((res) => {
              this.geList();
              loadingInstance.close();
              this.$message.success(res.data.message);
            })
            .catch((err) => {
              console.log(err);
              loadingInstance.close();
              this.$message.error("删除失败");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(e) {
      this.select = e.map((item) => item.id);
    },
    handleDelete(index, row) {
      console.log("删除数据");
      deleteQrCodeUrl([row["id"]])
        .then((res) => {
          if (res.data.code === 200) {
          this.$message.success('删除成功');
            return this.geList();
          }
          this.$message({
            type: "error",
            message: "删除失败",
          });
        })
        .catch((err) => {
          console.log(err);
          this.$message({
            type: "error",
            message: "删除失败",
          });
        });
    },
    geList() {
      getQRcode()
        .then((res) => {
          console.log(res);
          this.tableData = res.data.data;
          // this.totalPages = res.data.data["totalPages"];
          // console.log(this.totalPages);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    changeSelect(e) {
      this.search = "";
      // console.log(e);
      // this.searchLabel = e;
      this.options.some((item, index) => {
        if (item.value === e) {
          this.selectInput = item.label;
          if (!item.value) {
            this.selectInput = "";
            this.geList();
          }
          return true;
        }
      });
    },
    addLine() {
      let _this = this;
      this.$prompt("请输入路径", "添加二维码配置信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          addQrCodeUrl({ url: value })
            .then((res) => {
              _this.geList();
            })
            .catch((err) => {
              console.log(err);
              this.$message({
                type: "error",
                message: "添加失败",
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "您取消了",
          });
        });
    },
    handleUpdate(index, row) {
      this.updateItem = { ...row };
      this.updateShow = true;
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDefault(index, row) {
      console.log(index, row);
      this.$confirm("此操作将更改二维码默认路径, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let loadingInstance = Loading.service({
            fullscreen: true,
          });
          settingQrCodeUrl({ id: row["id"] })
            .then((res) => {
              this.geList();
              loadingInstance.close();
              this.$message.success("设置成功");
            })
            .catch((err) => {
              console.log(err);
              loadingInstance.close();
              this.$message.error("设置失败");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    change(e) {
      this.pageNum = e;
      this.geList();
    },
  },
};
</script>


<style lang="scss" scoped>
.app-container {
  height: 730px;
  overflow: hidden;
  .header-search {
    display: flex;
    width: 500px;
    > .header-search-title {
      width: 100px;
      color: #909399;
      font-size: 16px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    > .item {
      display: flex;
      overflow: hidden;
      width: 100%;
      justify-content: space-between;
      > .search-select {
        width: 250px;
        margin-right: 4px;
      }
      > .search-input {
        //width: 60px;
      }
      > .search-button {
        margin-left: 4px;
      }
    }
  }
  .footer-pagination {
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
